'use strict';

let router = require('koa-router')();

let fs = require('fs');

let path = require('path');



function create(){
    let dir = __dirname;

    let file = fs.readdirSync(dir);
    console.log(file);

    let files = file.filter(name=>{
        return name.endsWith('.js') && name !== 'index.js';
    });
    console.log(files);

    return files;
}

function routercreate(files){
    files.forEach(Element => {
        
        let filesPath = path.join(__dirname,Element);
        console.log(filesPath);
        
        let fullPath = require(filesPath);
        console.log(fullPath);

        for(let r in fullPath){
            let type = fullPath[r][0];
            console.log(type);
            let fn = fullPath[r][1];
            console.log(fn);

            if(type ==='get'){
               router.get(r,fn);
            }
        }
    });
}

module.exports = function(){
    let files = create();

    routercreate(files);

    return router.routes();
}